/*******************************************************************************
 * Copyright 2011 Mauro Luigi Drago (drago.luigi@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package com.bytenose.extrautils.logging;

import java.io.OutputStream;
import java.io.PrintWriter;

import com.bytenose.extrautils.annotations.NonNullable;

/**
 * A simple logger using a stream to log messages.
 * @author Mauro Luigi Drago
 *
 */
public final class StreamLogger implements ILogger, IStreamLogger {
	private static final String INFO_PREAMBLE 		= "INFO    ---> ";
	private static final String WARNING_PREAMBLE 	= "WARNING ---> ";
	private static final String ERROR_PREAMBLE 		= "ERROR   ---> ";

	/** The output stream */
	private final OutputStream os;
	
	/** The print writer used to log */
	private final PrintWriter pw;
	
	/** 
	 * A flag indicating if messages should be immediately 
	 * flushed when received.
	 */
	private final boolean autoFlush;
	
	/**
	 * Creates a new {@link StreamLogger} with auto flushing.
	 * 
	 * @param os the logging stream, cannot be null.
	 * @throws IllegalArgumentException
	 * 		if some of the non nullable arguments are null.
	 */
	public StreamLogger(@NonNullable OutputStream os) 
			throws IllegalArgumentException {
		if (os == null) throw new IllegalArgumentException("os");
		this.os = os;
		this.autoFlush = true;
		pw = new PrintWriter(os);
	}
	
	/**
	 * Creates a new {@link StreamLogger}.
	 * 
	 * @param os the logging stream, cannot be null.
	 * @param autoFlush 
	 * 		a flag indicating if messages should be immediately
	 * 		flushed when received.
	 * @throws IllegalArgumentException
	 * 		if some of the non nullable arguments are null.
	 */
	public StreamLogger(@NonNullable OutputStream os, boolean autoFlush) 
			throws IllegalArgumentException {
		if (os == null) throw new IllegalArgumentException("os");
		this.os = os;
		this.autoFlush = autoFlush;
		pw = new PrintWriter(os);
	}
	
	@Override
	public void logInfo(String message) {
		pw.print(INFO_PREAMBLE);
		pw.println(message);
		if (autoFlush) pw.flush();
	}

	@Override
	public void logWarning(String message) {
		pw.print(WARNING_PREAMBLE);
		pw.println(message);
		if (autoFlush) pw.flush();
	}

	@Override
	public void logError(String message) {
		pw.print(ERROR_PREAMBLE);
		pw.println(message);
		if (autoFlush) pw.flush();
	}

	@Override
	public void logError(Throwable e) {
		pw.print(ERROR_PREAMBLE);
		pw.println(e.getLocalizedMessage());
		if (autoFlush) pw.flush();
	}
	
	@Override
	public void logError(String message, Throwable e) {
		pw.print(INFO_PREAMBLE);
		pw.print(message);
		pw.println(e.getLocalizedMessage());
		if (autoFlush) pw.flush();
	}

	@Override
	public void dispose() {
		pw.flush();
		// Do not close the OutputStream or underlying print writer...
		// We do not have the ownership to do that
	}

	@Override
	public OutputStream getOutStream() {
		return os;
	}

	@Override
	public void flush() {
		pw.flush();
	}
}
